package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.adm.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.adm.TrainingPeopleMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.adm.TrainingPeople;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.adm.TrainingPeopleService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.adm.impl
 * @Description: ${TODO}(培训人员服务实现)
 * @author: lutianbo
 * @date: 2016/12/9,0009
 */

public class TrainingPeopleServiceImpl implements TrainingPeopleService{

        @Autowired
        private TrainingPeopleMapper peopleMapper;

        /**
         * 删除培训人员
         * @param trainingPeopleIds
         * @return
         */
        @Override
        public int deleteByPrimaryKey(String[] trainingPeopleIds){
            if (trainingPeopleIds.length>0){
                return  peopleMapper.deleteByPrimaryKey(trainingPeopleIds);
            }
            return 0;
         }

        /**
         * 新增培训人员
         * @param record
         * @return
         */
        @Override
        public int insertSelective(TrainingPeople record) {
            if(null != record){
                return  peopleMapper.insertSelective(record);
            }
            return 0;
        }

        /**
         * 根据id查询
         * @param trainingPeopleId
         * @return
         */
        @Override
        public TrainingPeople selectByPrimaryKey(String trainingPeopleId) {
            if (!"".equals(trainingPeopleId)){
                return  peopleMapper.selectByPrimaryKey(trainingPeopleId);
            }
            return null;
        }

        /**
         * 更新培训人员
         * @param record
         * @return
         */
        @Override
        public int updateByPrimaryKeySelective(TrainingPeople record){
            if(null != record){
                return  peopleMapper.updateByPrimaryKeySelective(record);
            }
            return 0;
        }

        /**
         * 培训人员列表
         * @param bootstrapSearchBean
         * @return
         */
        @Override
        public JSONObject list(BootstrapSearchBean bootstrapSearchBean) {
            JSONObject jsonObject = new JSONObject();
            // 分页设置
            bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? Integer.valueOf(10) : bootstrapSearchBean.getLimit() );
            bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? Integer.valueOf(0) : bootstrapSearchBean.getOffset() );
            // 总条数
            jsonObject.put("total",peopleMapper.queryCount(bootstrapSearchBean));
            List<TrainingPeople> trainList = peopleMapper.queryList(bootstrapSearchBean);
            jsonObject.put("rows", JSON.toJSON(trainList));
            return jsonObject;
         }
        /**
         * 根据培训计划id删除数据
         * @param trainingPlanIds
         * @return
         */
        public int deleteByTrainingPlanIds(String[] trainingPlanIds){
            return  peopleMapper.deleteByTrainingPlanIds(trainingPlanIds);
        }

    /**
     * 根据培训计划id查询人员
     * @param trainingId 培训计划id
     * @return
     */
    @Override
    public List<TrainingPeople> queryPeoPleByTrainingId(String trainingId)
    {
        return peopleMapper.queryPeoPleByTrainingId(trainingId);
    }

    @Override
    public int batchDel(List<TrainingPeople> oldExitPeople)
    {
        return peopleMapper.batchDel(oldExitPeople);
    }

    /**
     * 查看该人员是否已在该培训中
     * @param id 人员id
     * @param trainingId 计划id
     * @return
     */
    @Override
   public boolean isExistById(String id, String trainingId){
       Map<String,Object> map = new HashMap<>();
       map.put("empId",id);
       map.put("trainingId",trainingId);
       return peopleMapper.isExistById(map) > 0 ? true :false;
    }

    /**
     * 根据培训计划删除所关联人员
     * @param trainingId
     * @return
     */
    @Override
    public int delByTrainPlanId(String trainingId){
       return peopleMapper.delByTrainPlanId(trainingId);
    }
}
